package cn.haohaowo.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.haohaowo.common.Constants;
import cn.haohaowo.entity.Account;
import cn.haohaowo.service.AccountBizService;
import cn.haohaowo.service.impl.AccountBizServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;


public class LoginFilter extends AbstractFilter {

	@Autowired
	private AccountBizService accountBizService;

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;

		if (loginFromSession(request) || loginFromCookie(request)) {
			chain.doFilter(req, res);
		} else {
			storeReturnPath(request);
			request.getSession().setAttribute(Constants.LOGIN_MSG_KEY, "请登录");
			response.sendRedirect(request.getContextPath() + "/login");
		}
	}
	
	public boolean pathContains(String path) {

		return path.contains("order") || path.contains("my") || path.contains("modifyUserInfo");

	}
	
	private boolean loginFromCookie(HttpServletRequest request) {

		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(Constants.REMEMBER_ME_KEY)) {
					String value = cookie.getValue();
					String[] values = value.split(Constants.REMEMBER_ME_SEPERATOR);
					String username = values[0];
					String password = values[1];
					Account account = accountBizService.login(username, password);
					if (null != account) {
						getSessionContainer(request).setAccount(account);
						return true;
					}
				}
				break;
			}
		}

		return false;
	}

}
